<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    
    <title>Git | 王鹏的技术博客</title>
    
    
        <meta property="og:site_name" content="王鹏的技术博客">
    
    
        <meta property="article:author" content="王 鹏">
    
    
        <link rel="icon" href="/mhexo/img/favicon.ico">
    
    
<link rel="stylesheet" href="/mhexo/css/minireset.min.css">

    
<link rel="stylesheet" href="/mhexo/css/all.min.css">

    
<link rel="stylesheet" href="/mhexo/css/csshake.min.css">

    
<link rel="stylesheet" href="/mhexo/css/hljs/lioshi.css">

    
<link rel="stylesheet" href="/mhexo/css/jquery.fancybox.min.css">

    
<link rel="stylesheet" href="/mhexo/styl/main.css">

    
<script src="/mhexo/js/jquery.min.js"></script>

    
<script src="/mhexo/js/highlight.min.js"></script>

    
<script src="/mhexo/js/jquery.fancybox.min.js"></script>

    
<script src="/mhexo/js/clipboard.min.js"></script>

<meta name="generator" content="Hexo 5.2.0"></head>
<body>
    <header>
  <div class="outer">
    <div class="inner">
      <h1 class="logo-wrap">
        <a>王鹏的技术博客<b><sup>1.1</sup></b></a>
      </h1>
    </div>
    <div class="inner">
      <nav class="main-nav">
        
          <a href="/mhexo/">首页</a>
        
          <a href="/mhexo/archives">归档</a>
        
          <a href="/mhexo/categories">分类</a>
        
          <a href="/mhexo/tags">标签</a>
        
          <a href="/mhexo/friends">友链</a>
        
          <a href="/mhexo/about">关于</a>
        
          <a target="_blank" rel="noopener" href="https://www.baidu.com">百度</a>
        
      </nav>
    </div>
  </div>
</header>
    <div class="content">
        <section class="outer">
    <article>
        <div class="article-title">
    <h2>
        <a href="/mhexo/2020/12/15/git/" class="shake shake-little" title="Git">
            
            Git
        </a>
    </h2>
    <div class="meta_box">
    
        
        
            
                
        
        <div class="meta meta_auth">
            <img src="/mhexo/img/default.png" alt="head" />
            <p>anonymous</p>
        </div>
    
        
        
        <div class="meta meta_date">
            <i class="fas fa-calendar-alt fa-fw" aria-hidden="true"></i>
            <p>发布于：2020年12月15日</p>
        </div>
    
        <div class="meta meta_update">
            <i class="fas fa-edit fa-fw" aria-hidden="true"></i>
            <p>更新于：2020年12月15日</p>
        </div>
    </div>
    

</div>

        <hr>
        <div class="article-entry">
            
            
            
            <h2 id="1-git基本概念"><a href="#1-git基本概念" class="headerlink" title="1.git基本概念"></a>1.git<strong>基本概念</strong></h2><h3 id="1-1-常见版本管理工具"><a href="#1-1-常见版本管理工具" class="headerlink" title="1.1 常见版本管理工具"></a>1.1 <strong>常见版本管理工具</strong></h3><ul>
<li><p>git</p>
<ul>
<li>git是分布式的，每一个安装git环境的机器都有线下的版本仓库</li>
<li>开发者无需把代码提交到线上仓库（GitHub、gitee、gitlab） </li>
</ul>
</li>
<li><p>SVN </p>
<ul>
<li>SVN是一个集中的版本管理工具</li>
<li>开发者必须把代码提交到SVN服务器，本地没有版本仓库</li>
</ul>
</li>
</ul>
<h3 id="1-2-线上仓库"><a href="#1-2-线上仓库" class="headerlink" title="1.2 线上仓库"></a>1.2 <strong>线上仓库</strong></h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">多人协作开发时，会管理代码</span><br></pre></td></tr></table></figure>

<ul>
<li><p>github / gitee </p>
</li>
<li><p>svn </p>
</li>
</ul>
<h3 id="1-3-git原理"><a href="#1-3-git原理" class="headerlink" title="1.3 git原理"></a>1.3 git<strong>原理</strong></h3><p><strong>工作区：</strong> 就是你在电脑上看到的目录，比如目录下testgit里的文件(.git隐藏目录版本库除外)。</p>
<p><strong>暂存区</strong> : 暂存区就是文件夹 .git中的一个小部分（.git文件夹就是版本库）</p>
<p><strong>版本库：</strong>工作区有一个隐藏目录.git,这个不属于工作区，这是版本库， 版本库中还有Git为我们</p>
<p>![096f0450f2a2e9e36fa1b07a04d2f5d](C:\Users\Administrator\AppData\Local\Temp\WeChat Files\096f0450f2a2e9e36fa1b07a04d2f5d.png)</p>
<h2 id="2-gitee中使用"><a href="#2-gitee中使用" class="headerlink" title="2.gitee中使用"></a>2.gitee<strong>中使用</strong></h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"># Git 全局设置: </span><br><span class="line">git config --global user.name &quot;110&quot; </span><br><span class="line">git config --global user.email &quot;5462539+xiaonq110@user.noreply.gitee.com&quot; # 创建 git 仓库: </span><br><span class="line">mkdir git_test </span><br><span class="line">cd git_test </span><br><span class="line">git init </span><br><span class="line">touch README.md </span><br><span class="line">git add README.md </span><br><span class="line">git commit -m &quot;first commit&quot; </span><br><span class="line">git remote add origin https:&#x2F;&#x2F;gitee.com&#x2F;eduworkflow&#x2F;git_test.git</span><br><span class="line">git push -u origin master</span><br><span class="line"># 已有仓库?</span><br><span class="line">cd existing_git_repo</span><br><span class="line">git remote add origin https:&#x2F;&#x2F;gitee.com&#x2F;eduworkflow&#x2F;git_test.git</span><br><span class="line">git push -u origin master</span><br></pre></td></tr></table></figure>

<h2 id="3-git常见命令"><a href="#3-git常见命令" class="headerlink" title="3.git常见命令"></a>3.git<strong>常见命令</strong></h2><h3 id="1-简单提交"><a href="#1-简单提交" class="headerlink" title="1.简单提交"></a>1.简单提交</h3><p>git add xxx.txt # 蒋文件提交到暂存区 </p>
<p>git commit -m ‘xxx’ # 蒋代码提交到本地版本仓库 </p>
<p>git status # 看到未提交到仓库的修改文件（工作区、暂存区） </p>
<p>git log </p>
<h3 id="2-回滚"><a href="#2-回滚" class="headerlink" title="2.回滚"></a>2.回滚</h3><p>git log #查看所有提交到仓库的版本记录: git log -2 </p>
<p>git reflog #查看所有操作记录（状态的md5值和改变的值） </p>
<p>git reset –hard d9e0ed0 #回到指定版本（d9e0ed0是创建版本的MD5值得前6位或者7 </p>
<p>位）</p>
<p>git reset –hard HEAD^ #回到上一个版本</p>
<h3 id="3-撤销修改"><a href="#3-撤销修改" class="headerlink" title="3.撤销修改"></a>3.撤销修改</h3><p>vim Readme #我们在Readme文件中写了一些错误的代码 </p>
<p>git add . #然后又一不小心将文件从工作区提交到了 stage区 </p>
<p>git reset HEAD Readme #将Readme中刚提交到 stage区 的代码撤回到工作区 </p>
<p>git status #查看目前工作区状态 </p>
<p>git checkout – Readme #将Readme在工作区错误的代码丢弃</p>
<h3 id="4-强制使用master覆盖本地代码"><a href="#4-强制使用master覆盖本地代码" class="headerlink" title="4.强制使用master覆盖本地代码"></a>4.强制使用master覆盖本地代码</h3><p>git fetch –all </p>
<p>git reset –hard origin/master </p>
<p>git pull</p>
<h2 id="4-git分支管理"><a href="#4-git分支管理" class="headerlink" title="4.git分支管理"></a>4.git<strong>分支管理</strong></h2><h3 id="1-分支管理概念"><a href="#1-分支管理概念" class="headerlink" title="1 分支管理概念"></a>1 <strong>分支管理概念</strong></h3><ul>
<li>master 主分支，稳定代码，为生产环境做准备的</li>
<li>develop 开发分支，为开发服务</li>
</ul>
<p><img src="C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20201211075654651.png" alt="image-20201211075654651"></p>
<h3 id="2-分支使用"><a href="#2-分支使用" class="headerlink" title="2 分支使用"></a>2 <strong>分支使用</strong></h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">&#39;&#39;&#39;1. 从master分支切换到dev分支进行开发&#39;&#39;&#39; </span><br><span class="line"></span><br><span class="line">git checkout -b dev master # 从master分支创建dev开发分支 </span><br><span class="line"></span><br><span class="line">git branch # 可以看到现在已经切换到dev分支了 </span><br><span class="line"></span><br><span class="line">vim Readme # 模拟在项目中修改代码 </span><br><span class="line"></span><br><span class="line">git add . # 把工作区中所有变更全部提交到（暂存区） </span><br><span class="line"></span><br><span class="line">git commit -m &#39;in dev&#39; # 把暂存区代码提交到本地git仓库（本地git仓库） </span><br><span class="line"></span><br><span class="line">git push origin dev # 把代码先推倒dev分支，让测试人员测试 </span><br><span class="line"></span><br><span class="line">&#39;&#39;&#39;2. 当测试人员测试完成后将dev分支代码合并到master形成文档版本&#39;&#39;&#39; </span><br><span class="line"></span><br><span class="line">git checkout master # 开发完成后，需要合并回master分支，先切换到master分支 </span><br><span class="line"></span><br><span class="line">cat Readme # 可以看到master分支的内容没有修改 </span><br><span class="line"></span><br><span class="line">git merge --no-ff dev # 将刚刚dev中修改的代码合并到master </span><br><span class="line"></span><br><span class="line">git push origin master # 将本地仓库的代码推送到线上仓库（线上git仓库）</span><br></pre></td></tr></table></figure>




















        </div>
        <div class="article-copyright">
            
    <blockquote>
        <p>
            版权声明：本文为「子曰无衣」的原创文章，博客内容遵循 署名-非商业性使用-相同方式共享 协议。<br>本文永久链接是：https://wangpdizhi.gitee.io/mhexo/2020/12/15/git/
        </p>
    </blockquote>


        </div>
    </article>
    

    <section id="toc-div" >
        <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-git%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="toc-text">1.git基本概念</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1-%E5%B8%B8%E8%A7%81%E7%89%88%E6%9C%AC%E7%AE%A1%E7%90%86%E5%B7%A5%E5%85%B7"><span class="toc-text">1.1 常见版本管理工具</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-%E7%BA%BF%E4%B8%8A%E4%BB%93%E5%BA%93"><span class="toc-text">1.2 线上仓库</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-git%E5%8E%9F%E7%90%86"><span class="toc-text">1.3 git原理</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-gitee%E4%B8%AD%E4%BD%BF%E7%94%A8"><span class="toc-text">2.gitee中使用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-git%E5%B8%B8%E8%A7%81%E5%91%BD%E4%BB%A4"><span class="toc-text">3.git常见命令</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E7%AE%80%E5%8D%95%E6%8F%90%E4%BA%A4"><span class="toc-text">1.简单提交</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E5%9B%9E%E6%BB%9A"><span class="toc-text">2.回滚</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E6%92%A4%E9%94%80%E4%BF%AE%E6%94%B9"><span class="toc-text">3.撤销修改</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-%E5%BC%BA%E5%88%B6%E4%BD%BF%E7%94%A8master%E8%A6%86%E7%9B%96%E6%9C%AC%E5%9C%B0%E4%BB%A3%E7%A0%81"><span class="toc-text">4.强制使用master覆盖本地代码</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-git%E5%88%86%E6%94%AF%E7%AE%A1%E7%90%86"><span class="toc-text">4.git分支管理</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E5%88%86%E6%94%AF%E7%AE%A1%E7%90%86%E6%A6%82%E5%BF%B5"><span class="toc-text">1 分支管理概念</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E5%88%86%E6%94%AF%E4%BD%BF%E7%94%A8"><span class="toc-text">2 分支使用</span></a></li></ol></li></ol>
    </section>
    <section id="gohome" style="display: none;"><a>顶</a></section>

    <script>
        function get_top_by_link(link){
            var hnid = "#" + $(link).attr("data");
            if ($(hnid).length > 0){
                return $(hnid).offset().top;
            }else{
                return 0;
            }
        }
        //go to hn
        function gotohn(link){
            $("html,body").animate({scrollTop: get_top_by_link(link) }, 300);
        }
        //页面滚动
        function update(){
            var scrollH = $(window).scrollTop();
            if($(".toc-link")){
                $(".toc-link").each(function(i,link){
                    var mdHeight = get_top_by_link(link);
                    if(mdHeight <= scrollH + 40){
                        //高亮导航菜单
                        $('.toc-link').removeClass('on');
                        $(link).addClass('on');
                    }
                });
            }
            //返回顶部显隐
            if(scrollH < 200){
                $("#gohome").css("display","none");
            }else{
                $("#gohome").css("display","block");
            }
        }
        $(function(){
            //修复部分锚点从属关系
            if($("#toc-div >li").length > 0){
                $("#toc-div >li").appendTo($("#toc-div >ol:first"));
            }
            //返回顶部
            $('#gohome').click(function(){
                $("html,body").animate({scrollTop: 0}, 300);
                return false;
            })
            //遍历锚点
            $(".toc-link").each(function(i,link){
                $(link).attr("data",$(link).attr('href').substring(1));
                $(link).attr("href","javascript:void(0);");
                $(link).attr("onclick","gotohn(this);");
            })
            //绑定滚动事件
            $(window).bind('scroll', update);
            //初始化toc
            var first_toc = $(".toc-link")[0];
            var first_scroll = get_top_by_link(first_toc);
            var window_scroll = $(window).scrollTop();
            if(window_scroll <= first_scroll){
                $(first_toc).addClass('on');
            }
        })
    </script>

</section>
    </div>
    <footer>
    <div class="outer">
        <div class="inner">
            Powered by <a href="http://prowiki.demopage.icu/" target="_blank">ProWiki</a>
            &copy;2020 子曰无衣<br>
            <a target="_blank" rel="noopener" href="http://beian.miit.gov.cn/">冀ICP备00000000号</a>
        </div>
    </div>
</footer>

<script src="/mhexo/js/custom.js"></script>


    <script>onload_content();</script>

</body>
</html>
